
import { saveBoarding } from '../util/BoardingUtil';
import Constants from './Constants'
import { post } from './HiNet'

/**
 * 登录模块相关网络服务
 */
export default class LoginApi{
    private static instance: LoginApi;
    private constructor() { }
    public static getInstance(): LoginApi {
        if (!LoginApi.instance) {
            LoginApi.instance = new LoginApi();
        }
    
        return LoginApi.instance;
      }
      login(userName: string, password: string): Promise<any> {
        return new Promise((resolve, reject) => {
          const {
            login: { api },
          } = Constants;
          const formData = new FormData();
          formData.append('userName', userName);
          formData.append('password', password);
          post(api)(formData)({userName,password}).then((res: any) => {
            const { code, data, msg } = res;
            if (code === 0) {
              saveBoarding(data);
              resolve(data || msg);
            } else {
              reject(res);
            }
          })
            .catch((e) => {
              console.log(e);
              reject({ code: -1, msg: '哎呀出错了' });
            });
        });
      }
    
}